/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.input;


public class WipeDetector implements TouchListener {

    protected boolean exclusive = true;

    protected float startX;
    protected float startY;
    protected int pointer;

    protected WipeListener listener;
    protected float minDistance;
    protected boolean running;
    protected boolean continious;

    public WipeDetector(WipeListener listener) {
        this.listener = listener;
    }

    public WipeDetector(float minDistance, WipeListener listener) {
        this.minDistance = minDistance;
        this.listener = listener;
    }

    public float getMinDistance() {
        return minDistance;
    }

    public WipeDetector setMinDistance(float minDistance) {
        this.minDistance = minDistance;
        return this;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    /**
     * If true, will consume the event and no other touchlistener will be notified.
     * Set to false, if you want to have more draggable layers for example.
     *
     * @param exclusive
     * @return
     */
    public WipeDetector setExclusive(final boolean exclusive) {
        this.exclusive = exclusive;
        return this;
    }

    public boolean isContinious() {
        return continious;
    }

    /**
     * Will continue fireing onWipe events, event after minDistance is reached.
     *
     * Default is continious=false
     * @param continious true = does not stop after reaching minDistance
     * @return
     */
    public WipeDetector setContinious(boolean continious) {
        this.continious = continious;
         return this;
    }

    @Override
    public boolean onTouchDown(float x, float y, int pointer, boolean isHit) {
        startX = x;
        startY = y;
        this.pointer = pointer;
        running = true;

        return exclusive;
    }

    @Override
    public boolean onTouchUp(float x, float y, int pointer, boolean isHit) {
        return false;
    }

    @Override
    public boolean onTouchDragged(float x, float y, int pointer, boolean isHit) {
        if (pointer != this.pointer || (!running && !continious))
            return false;

        final float diffx = (x - startX);
        final float diffy = (y - startY);

        if (minDistance != 0) {
            if ((diffx > 0 && diffx < minDistance) || (diffx < 0 && diffx > -minDistance) ||
                    (diffy > 0 && diffy < minDistance) || (diffy < 0 && diffy > -minDistance))
                return false;
        }

        listener.onWipe(diffx, diffy);

        if (diffx < 0) {
            if (diffy == 0 || (diffy < 0 && diffx < diffy) || (diffy > 0 && (diffx < -diffy))) {
                listener.onWipeLeft(diffx, diffy);
                running = false;
            } else if (diffy < 0) {
                listener.onWipeBottom(diffx, diffy);
                running = false;
            } else {
                listener.onWipeTop(diffx, diffy);
                running = false;
            }
        } else if (diffx > 0) {
            if (diffy == 0 || (diffy < 0 && -diffx < diffy) || (diffy > 0 && diffx > diffy)) {
                listener.onWipeRight(diffx, diffy);
                running = false;
            } else if (diffy < 0) {
                listener.onWipeBottom(diffx, diffy);
                running = false;
            } else {
                listener.onWipeTop(diffx, diffy);
                running = false;
            }

        } else {
            if (diffy < 0) {
                listener.onWipeBottom(diffx, diffy);
                running = false;
            } else {
                listener.onWipeTop(diffx, diffy);
                running = false;
            }
        }

        return exclusive;

    }


}